<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\index\controller;

use api\index\model\ImgCompress;
use cmf\controller\RestBaseController;
use cmf\lib\Storage;

class UploadController extends RestBaseController
{
    protected $noNeedLogin = ['uploadFile'];

    //上传文件一个或多个
    public function uploadFile(){
        if($this->request->isPost()){
            $files = request()->file('resource');
            if (empty($files)) {
                $this->error('未检出文件上传');
            }
            $countFile = count($files);
            if($countFile > 9){
                $this->error('最多上传9张图片');
            }
            $date = date('Ymd',time());
            $host = config('verify.host');
            $arr = [];
            foreach($files as $k=>$file){
                //移动到框架应用根目录/public/upload/api/ 目录下
                $move_url = \think\facade\Env::get('root_path').'/public/upload/api/';
                //允许文件大小20M
                $info = $file->validate(['size'=>20480000,'ext'=>'jpg,png,gif,mp3,pcm,wma,wav,mp4,avi,wmv,rm,rmvb,mkv,mov,ts'])->move($move_url);
                if($info){
                    $image_relation_url = 'upload/api/'.$date.'/'.$info->getFilename();//图片相对路径
                    $fileInfo = $file->getinfo();
                    $img_url = \think\facade\Env::get('root_path').'/public/'.$image_relation_url;
                    if($fileInfo['type'] == 'image/jpeg' || $fileInfo['type'] == 'image/png'){
                        $percent = 1;
                        //判断像素不超过10M---10485760字节
                        $pixel = getimagesize ($img_url);
                        $with = $pixel[0];
                        $height = $pixel[1];
                        $w_h = $with*$height;
                        $can_Compress = true;
                        if($w_h > 10485760){
                            $can_Compress = false;
                            //长宽太大，超过服务器内存限制，不可以压缩
                            if($fileInfo['size'] > 1024000)
                            {
                                //文件大小超过1M，不允许上传
                                $this->error('文件像素过大');
                            }
                        }
                        if($can_Compress && $fileInfo['size'] > 512000) {
                            //可以压缩，且文件大小超过512K，进行压缩
                            $percent = round(sqrt(512000 / $fileInfo['size']), 4);//压缩
                        }
                        if($percent<1) {
                            (new ImgCompress($img_url, $percent))->compressImg($img_url);
                        }
                    }

                    //上传至七牛云文件路径
                    $qiniu_file = 'api/'.$date.'/'.$info->getFilename();
                    $storage = new Storage();
                    $qi_res = $storage->upload($qiniu_file, $img_url, 'image');
                    if($qi_res){
                        $arr[$k]['image_url'] = $host.'api/'.$date.'/'.$info->getFilename();
                        $sys = $this->getOperateSys();
                        if($sys == 'Linux'){
                            unlink($img_url);//适用于linux
                        }
                    }else{
                        $this->error('上传七牛云出错');
                    }
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            $this->success('上传成功',['data'=>$arr]);
        }else{
            $this->error('请求方式错误');
        }
    }

    //判断当前操作系统
    public function getOperateSys(){
        $os_name = php_uname('s');
        //判断
        if(strpos($os_name,"Linux")!==false){
            $os_str="Linux";
        }else if(strpos($os_name,"Windows")!==false){
            $os_str="Windows";
        }else{
            $os_str='';
        }
        return $os_str;
    }
}
